export function filterTreeData(params) {

    var r =[],
        deps=null

    if('data' in params){
        if (params.data.personnelOfWithOutDepartment!=null)
        {
            params.data.personnelOfWithOutDepartment.forEach(e=>
            {
                r.push(e);
            })
        }
        deps = traverseDep(params.data.rootDepartments)
    }else{
        deps = traverseDep(params)
    }

    // debugger
    // if (params.data.personnelOfWithOutDepartment!=null)
    // {
    //     params.data.personnelOfWithOutDepartment.forEach(e=>
    //     {
    //         r.push(e);
    //     })
    // }

    if (deps!=null)
    {
        deps.forEach(d=>
        {
            r.push(d);
        })
    }
    return r
}

// 根据组织ID获取完整的上级ID
export function getParentsById(list, id) {
    for (let i in list) {
        if (list[i].id == id) {
            //查询到就返回该数组对象的value
            return [list[i].id];
        }
        if (list[i].departmentInfos) {
            let node = getParentsById(list[i].departmentInfos, id);
            if (node !== undefined) {
                //查询到把父节把父节点加到数组前面
                node.unshift(list[i].id);
                return node;
            }
        }
    }
}
export function traverseDep(deps) {
    var result = [];
    if (deps!=null)
    {
        deps.forEach(e=>{
            e.children=[];
            if (e.personnelInfos!=null)
            {
                e.personnelInfos.forEach(p=>
                {
                    e.children.push(p);
                })
            }

            if (e.departmentInfos!=null)
            {
                e.departmentInfos.forEach(d=>
                {
                    e.children.push(d);

                })
                traverseDep(e.departmentInfos);
            }


            e.personnelInfos=null;
            e.departmentInfos=null;
            result.push(e);
        })
    }
    return result;
}

// 递归判断列表，把最后空children设为undefined
export function filterLastTreeData(data){
    for(var i=0;i<data.length;i++){
        if(data[i].departmentInfos.length<1){
            // children若为空数组，则将children设为undefined
            data[i].departmentInfos=undefined;
        }else {
            // children若不为空数组，则继续 递归调用 本方法
            filterLastTreeData(data[i].departmentInfos);
        }
    }
    return data;
}